package com.zhiche.lisa.bms.controller.psup;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.dao.model.psup.PsupBaseData;
import com.zhiche.lisa.bms.pojo.vo.common.CommonVO;
import com.zhiche.lisa.bms.pojo.vo.psup.PsupBaseDataVO;
import com.zhiche.lisa.bms.service.psup.PsupBaseDataService;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 人送供方基础信息维护 前端控制器
 * </p>
 *
 * @author linbao
 * @since 2019-06-18
 */
@RestController
@RequestMapping("/psupBaseData")
@Api(description = "人送供方基础信息", tags = "人送供方基础信息")
@Slf4j
public class PsupBaseDataController extends PsupSdBaseController {

    @Autowired
    private PsupBaseDataService psupBaseDataService;

    /**
     * 分页查询
     *
     * @return
     */
    @PostMapping(value = "/querySdBaseDataInfoPage")
    @ApiOperation(value = "人送基础信息的分页查询", notes = "人送基础信息的分页查询", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PsupBaseDataVO.class),
            @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<PsupBaseDataVO>> querySdBaseDataInfoPage(@RequestBody Page<PsupBaseData> page) {
        log.info("PsupBaseDataController.querySdBaseDataInfoPage param: {}", page);
        RestfulResponse<Page<PsupBaseDataVO>> resultResponse = getSuccessResponse();
        try {
            resultResponse.setData(psupBaseDataService.querySdBaseDataInfoPage(page));
        } catch (BaseException e) {
            log.error("PsupBaseDataController.querySdBaseDataInfoPage error:{}", e);
            setBaseExceptionResponse(resultResponse, e.getMessage());
        } catch (Exception e) {
            log.error("PsupBaseDataController.querySdBaseDataInfoPage error:{} ", e);
            setErrorResponse(resultResponse);
        }
        return resultResponse;
    }

    /**
     * 人送基础信息的新增
     *
     * @return
     */
    @PostMapping(value = "/createSdBaseDataInfo")
    @ApiOperation(value = "人送基础信息的新增", notes = "人送基础信息的新增", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = RestfulResponse.class),
            @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Object> createSdBaseDataInfo(@RequestBody PsupBaseData psupBaseData) {
        log.info("PsupBaseDataController.createSdBaseDataInfo param: {}", psupBaseData);
        RestfulResponse<Object> resultResponse = getSuccessResponse();
        try {
            psupBaseDataService.createSdBaseDataInfo(psupBaseData);
        } catch (BaseException e) {
            log.error("PsupBaseDataController.createSdBaseDataInfo error:{}", e);
            setBaseExceptionResponse(resultResponse, e.getMessage());
        } catch (Exception e) {
            log.error("PsupBaseDataController.createSdBaseDataInfo error:{} ", e);
            setErrorResponse(resultResponse);
        }
        return resultResponse;
    }

    /**
     * 人送基础信息的修改
     *
     * @return
     */
    @PostMapping(value = "/updateSdBaseDataInfo")
    @ApiOperation(value = "人送基础信息的修改", notes = "人送基础信息的修改", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = RestfulResponse.class),
            @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Object> updateSdBaseDataInfo(@RequestBody PsupBaseData psupBaseData) {
        log.info("PsupBaseDataController.updateSdBaseDataInfo param: {}", psupBaseData);
        RestfulResponse<Object> resultResponse = getSuccessResponse();
        try {
            psupBaseDataService.updateSdBaseDataInfo(psupBaseData);
        } catch (BaseException e) {
            log.error("PsupBaseDataController.updateSdBaseDataInfo error:{}", e);
            setBaseExceptionResponse(resultResponse, e.getMessage());
        } catch (Exception e) {
            log.error("PsupBaseDataController.updateSdBaseDataInfo error:{} ", e);
            setErrorResponse(resultResponse);
        }
        return resultResponse;
    }

    /**
     * 人送基础信息的审核
     *
     * @return
     */
    @PostMapping(value = "/auditSdBaseDataInfo")
    @ApiOperation(value = "人送基础信息的审核", notes = "人送基础信息的审核", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = RestfulResponse.class),
            @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Object> auditSdBaseDataInfo(@RequestBody CommonVO<Long> commonVO) {
        log.info("PsupBaseDataController.auditSdBaseDataInfo param: {}", commonVO);
        RestfulResponse<Object> resultResponse = getSuccessResponse();
        try {
            if (Objects.isNull(commonVO) || CollectionUtils.isEmpty(commonVO.getEntityParams())) {
                throw new BaseException("参数为空, 请选择数据!");
            }
            List<String> resultMst = psupBaseDataService.auditSdBaseDataInfo(commonVO.getEntityParams());
            if (CollectionUtils.isNotEmpty(resultMst)) {
                setErrorListResponse(resultResponse, resultMst);
                return resultResponse;
            }
        } catch (BaseException e) {
            log.error("PsupBaseDataController.auditSdBaseDataInfo error:{}", e);
            setBaseExceptionResponse(resultResponse, e.getMessage());
        } catch (Exception e) {
            log.error("PsupBaseDataController.auditSdBaseDataInfo error:{} ", e);
            setErrorResponse(resultResponse);
        }
        return resultResponse;
    }

    /**
     * 根据id获取对应的人送基础数据
     *
     * @return
     */
    @GetMapping(value = "/getPsupBaseDataInfoById/{id}")
    @ApiOperation(value = "人送基础信息的审核", notes = "人送基础信息的审核", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = RestfulResponse.class),
            @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<PsupBaseDataVO> getPsupBaseDataInfoById(@PathVariable("id") Long id) {
        log.info("PsupBaseDataController.getPsupBaseDataInfoById param: {}", id);
        RestfulResponse<PsupBaseDataVO> resultResponse = getSuccessResponse();
        try {
            PsupBaseData psupBaseData = psupBaseDataService.selectById(id);
            PsupBaseDataVO vo = new PsupBaseDataVO();
            BeanUtils.copyProperties(psupBaseData, vo);
            resultResponse.setData(vo);
        } catch (BaseException e) {
            log.error("PsupBaseDataController.getPsupBaseDataInfoById error:{}", e);
            setBaseExceptionResponse(resultResponse, e.getMessage());
        } catch (Exception e) {
            log.error("PsupBaseDataController.getPsupBaseDataInfoById error:{} ", e);
            setErrorResponse(resultResponse);
        }
        return resultResponse;
    }

    /**
     * 人送基础信息的删除
     *
     * @return
     */
    @PostMapping(value = "/deleteSdBaseDataInfo")
    @ApiOperation(value = "人送基础信息的删除", notes = "人送基础信息的删除", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = RestfulResponse.class),
            @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Object> deleteSdBaseDataInfo(@RequestBody CommonVO<Long> commonVO) {
        log.info("PsupBaseDataController.deleteSdBaseDataInfo param: {}", commonVO);
        RestfulResponse<Object> resultResponse = getSuccessResponse();
        try {
            if (Objects.isNull(commonVO) || CollectionUtils.isEmpty(commonVO.getEntityParams())) {
                throw new BaseException("参数为空, 请选择数据!");
            }
            psupBaseDataService.deleteSdBaseDataInfo(commonVO.getEntityParams());
        } catch (BaseException e) {
            log.error("PsupBaseDataController.deleteSdBaseDataInfo error:{}", e);
            setBaseExceptionResponse(resultResponse, e.getMessage());
        } catch (Exception e) {
            log.error("PsupBaseDataController.deleteSdBaseDataInfo error:{} ", e);
            setErrorResponse(resultResponse);
        }
        return resultResponse;
    }
}